//
//  LoginView.m
//  CCJD
//
//  Created by foreveross－bj on 14/12/20.
//  Copyright (c) 2014年 foreveross－bj. All rights reserved.
//

#import "LoginView.h"

#define kPTHeight  30
#define kTISpace   10
#define kITSpace   20
#define kIHeight   1
#define kTIHeight  30
#define kFWidth    80
#define kFHeight   20
#define kLWidth    200
#define kLHeight   40
#define kTBSpace   100

@interface LoginView ()

@property (nonatomic, strong) UILabel *forgetLabel;                 //忘记密码按钮
@property (nonatomic, strong) UILabel *loginButton;                 //登录按钮

@end

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect rect = frame;
        rect.size.height = kPTHeight * 2 + kIHeight * 2 + kTISpace * 2 + kITSpace  + kTBSpace + kLHeight;
        self.frame = rect;
        
        //初始化子视图
        [self initView];
    }
    return self;
}

/**
 * 添加view
 **/
- (void)initView{
    //添加手机号输入框
    _phoneTextField = [[CustomTextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, kPTHeight)];
    _phoneTextField.placeholder = @"请输入手机号";
    _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView * phoneImageVIew = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kTIHeight, kTIHeight)];
    phoneImageVIew.image = [UIImage imageNamed:@"phone"];
    _phoneTextField.leftView = phoneImageVIew;
    _phoneTextField.textColor = [UIColor whiteColor];
    [self addSubview:_phoneTextField];
    
    //添加手机号输入框下划线
    UIImageView *phoneUnderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kPTHeight + kTISpace, self.frame.size.width, kIHeight)];
    phoneUnderImageView.backgroundColor = [UIColor whiteColor];
    [self addSubview:phoneUnderImageView];
    
    //添加密码输入框
    _passwordTextField = [[CustomTextField alloc] initWithFrame:CGRectMake(0, kPTHeight + kIHeight + kTISpace + kITSpace, self.frame.size.width, kPTHeight)];
    _passwordTextField.placeholder = @"请输入密码";
    _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kTIHeight, kTIHeight)];
    passwordImageView.image = [UIImage imageNamed:@"lock"];
    _passwordTextField.leftView = passwordImageView;
    _passwordTextField.textColor = [UIColor whiteColor];
    _passwordTextField.secureTextEntry = YES;
    [self addSubview:_passwordTextField];
    
    //添加密码输入框下划线
    UIImageView *passwordUnderImageVIew = [[UIImageView alloc] initWithFrame:CGRectMake(0, kPTHeight * 2 + kIHeight + kTISpace * 2 + kITSpace, self.frame.size.width, kIHeight)];
    passwordUnderImageVIew.backgroundColor = [UIColor whiteColor];
    [self addSubview:passwordUnderImageVIew];
    
    //添加忘记密码按钮
    self.forgetLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width - kFWidth, kPTHeight * 2 + kIHeight * 2 + kTISpace * 3 + kITSpace, kFWidth, kFHeight)];
    _forgetLabel.textColor = [UIColor whiteColor];
    _forgetLabel.textAlignment = NSTextAlignmentCenter;
    _forgetLabel.font = [UIFont systemFontOfSize:12];
    _forgetLabel.text = @"忘记密码?";
    UITapGestureRecognizer *forgetTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(forgetPassword:)];
    [_forgetLabel addGestureRecognizer:forgetTap];
    _forgetLabel.userInteractionEnabled = YES;
    [self addSubview:_forgetLabel];
    
    
    //添加登录按钮
    self.loginButton = [[UILabel alloc] initWithFrame:CGRectMake((self.frame.size.width - kLWidth) / 2, kPTHeight * 2 + kIHeight * 2 + kTISpace * 2 + kITSpace  + kTBSpace, kLWidth, kLHeight)];
    self.loginButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    _loginButton.text = @"登录";
    _loginButton.textAlignment = NSTextAlignmentCenter;
    _loginButton.font = [UIFont systemFontOfSize:20];
    _loginButton.textColor = [UIColor whiteColor];
    UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(login:)];
    [_loginButton addGestureRecognizer:loginTap];
    _loginButton.userInteractionEnabled = YES;
    [self addSubview:_loginButton];
}

/**
 * 点击忘记密码会执行此方法跳转到修改密码页面
 * @param tapGesture
 **/
- (void)forgetPassword:(UITapGestureRecognizer *)tapGesure{
    if (_forgetPassword) {
        _forgetPassword();
    }
}
/**
 * 点击登录按钮后会执行此方法实现登录功能
 * @param tapGesture
 **/
- (void)login:(UITapGestureRecognizer *)tapGesture{
    if (_doLogin) {
        _doLogin(_phoneTextField.text, _passwordTextField.text);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
